﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace TST.SharePointObjects
{
    public static class Globals
    {
        public const int NAMESEARCHLENGTH = 50;
        public const string MENUFEATUREID = "A2D3E4E5-34AB-4c2d-8BCE-44271AF94542";
        public const String NOTAPPLICABLE = "N/A";

        public static string GetReferenceTypeDescription(ObjectReferenceType objectReferenceType)
        {
            switch (objectReferenceType)
            {
                case ObjectReferenceType.ContentTypeList:
                    return "Content type associated to a list";
                case ObjectReferenceType.ContentTypeListItem:
                    return "Content type of a list item";
                case ObjectReferenceType.SiteColumnList:
                    return "Site column associated to a list";
                case ObjectReferenceType.SiteColumnListItem:
                    return "Site column used by a list item";
                case ObjectReferenceType.SiteColumnContentType:
                    return "Site column associated to a content type";
                case ObjectReferenceType.ContentTypeParent:
                    return "Content type is parent of a content type";
                case ObjectReferenceType.SharePointGroupWeb:
                    return "Role assignment to a site";
                case ObjectReferenceType.SharePointGroupList:
                    return "Role assignment to a list";
                case ObjectReferenceType.SharePointGroupFolder:
                    return "Role assignment to a folder";
                case ObjectReferenceType.SharePointGroupItem:
                    return "Role assignment to a list item";
                case ObjectReferenceType.FeatureDefinitionFarm:
                    return "Feature activation for this farm";
                case ObjectReferenceType.FeatureDefinitionWebApplication:
                    return "Feature activation for a web application";
                case ObjectReferenceType.FeatureDefinitionSite:
                    return "Feature activation for a site collection";
                case ObjectReferenceType.FeatureDefinitionWeb:
                    return "Feature activation for a site";
                default:
                    return string.Empty;
            }
        }

        public static string GetScopeDescription(ObjectReferenceScope scope)
        {
            switch (scope)
            {
                case ObjectReferenceScope.Farm:
                    return "Farm";
                case ObjectReferenceScope.SiteCollection:
                    return "Site Collection";
                case ObjectReferenceScope.WebApplication:
                    return "Web Application";
                case ObjectReferenceScope.Web:
                    return "Site";
                default:
                    return scope.ToString();
            }
        }

        internal static List<string> GetDemoContentTypes()
        {
            List<string> result = new List<string>();
            result.Add("0x0101"); // Document
            result.Add("0x0102"); // Event
            result.Add("0x0104"); // Announcement
            result.Add("0x0108"); // Task
            result.Add("0x010a"); // Person
            return result;
        }

        internal static string IndexByPropertyDescription(IndexByProperty indexByProperty)
        {
            switch (indexByProperty)
            {
                case IndexByProperty.Id:
                    return "Id";
                case IndexByProperty.InternalName:
                    return "Internal name";
                case IndexByProperty.DisplayName:
                    return "Display name";
                default:
                    return indexByProperty.ToString();
            }
        }

        internal static string GetRelativeUrl(string url)
        {
            if (string.IsNullOrEmpty(url))
                return string.Empty;
            if (url.Trim() == "/")
            {
                return string.Empty;
            }
            else
            {
                return url;
            }
        }

        internal static string GetItemUrl(string viewUrl, SPListItem item, String itemUrl)
        {
            string url = viewUrl;
            int p = url.ToLower().IndexOf("/forms/");
            if (p > 0)
            {
                url = url.Substring(0, p + 6);
                url += string.Format("/DispForm.aspx?ID={0}", item.ID);
            }
            else if (url.ToLower().EndsWith(".aspx"))
            {
                url = url.Substring(0, url.LastIndexOf("/") + 1);
                url += string.Format("DispForm.aspx?ID={0}", item.ID);
            }
            else
            {
                url = itemUrl;
            }
            return url;
        }

        internal static bool MatchingScope(SiteObject siteObject, Microsoft.SharePoint.Administration.SPWebApplication webApplication, SPSite site, SPWeb web)
        {
            bool match = false;
            switch (siteObject.IndexScope)
            {
                case ObjectReferenceScope.Farm:
                    match = true;
                    break;
                case ObjectReferenceScope.WebApplication:
                    match = siteObject.WebApplicationId == webApplication.Id;
                    break;
                case ObjectReferenceScope.SiteCollection:
                    match = siteObject.SiteId == site.ID;
                    break;
                case ObjectReferenceScope.Web:
                    match = siteObject.WebId == web.ID;
                    break;
            }
            return match;
        }

        internal static bool MatchingScope(SiteObject siteObject, Microsoft.SharePoint.Administration.SPWebApplication webApplication)
        {
            bool match = false;
            switch (siteObject.IndexScope)
            {
                case ObjectReferenceScope.Farm:
                    match = true;
                    break;
                case ObjectReferenceScope.WebApplication:
                    match = siteObject.WebApplicationId == webApplication.Id;
                    break;
            }
            return match;
        }

        internal static string GetReferenceId(SiteObject siteObject, SPField field)
        {
            string refId = string.Empty;
            switch (siteObject.IndexByProperty)
            {
                case IndexByProperty.DisplayName:
                    refId = field.Title;
                    break;
                case IndexByProperty.InternalName:
                    refId = field.InternalName;
                    break;
                case IndexByProperty.Id:
                    refId = field.Id.ToString("B");
                    break;
            }
            return refId;
        }

        internal static string GetReferenceId(SiteObject siteObject, SPFeature feature)
        {
            string refId = string.Empty;
            switch (siteObject.IndexByProperty)
            {
                case IndexByProperty.DisplayName:
                    refId = feature.Definition.DisplayName;
                    break;
                case IndexByProperty.InternalName:
                    refId = feature.Definition.Name;
                    break;
                case IndexByProperty.Id:
                    refId = feature.DefinitionId.ToString("B");
                    break;
            }
            return refId;
        }

        internal static string GetReferenceId(SiteObject siteObject, SPGroup group)
        {
            string refId = string.Empty;
            switch (siteObject.IndexByProperty)
            {
                case IndexByProperty.DisplayName:
                    refId = group.Name;
                    break;
                case IndexByProperty.InternalName:
                    refId = group.Name;
                    break;
                case IndexByProperty.Id:
                    refId = group.ID.ToString();
                    break;
            }
            return refId;
        }

        internal static string GetReferenceId(SiteObject siteObject, SPContentType contentType)
        {
            string refId = string.Empty;
            switch (siteObject.IndexByProperty)
            {
                case IndexByProperty.DisplayName:
                    refId = contentType.Name;
                    break;
                case IndexByProperty.InternalName:
                    refId = contentType.Name;
                    break;
                case IndexByProperty.Id:
                    refId = contentType.Id.ToString();
                    break;
            }
            return refId;
        }

        internal static string GetItemTitle(SPListItem item)
        {
            string title = string.Format("Item ID={0}", item.ID);
            if (item.File != null)
            {
                title = item.File.Name;
            }
            else if (!string.IsNullOrEmpty(item.Title))
            {
                title = item.Title;
            }
            return title;
        }

        internal static SiteObjectType GetSiteObjectTypeFromUrl(Uri uri)
        {
            string url = uri.ToString().ToLower();
            if (url.Contains("sofeaturedefinition.aspx"))
                return SiteObjectType.FeatureDefinition;
            else if (url.Contains("socontenttype.aspx"))
                return SiteObjectType.ContentType;
            else if (url.Contains("sosharepointgroup.aspx"))
                return SiteObjectType.SharePointGroup;
            else
                return SiteObjectType.SiteColumn;
        }
    }

    public enum ObjectReferenceScope
    {
        Web,
        SiteCollection,
        WebApplication,
        Farm
    }

    public enum IndexByProperty
    {
        Id,
        InternalName,
        DisplayName
    }
}
